package cn.highedu.coolshark.product.service.impl;

import cn.highedu.coolshark.common.EntityNotFoundException;
import cn.highedu.coolshark.common.EntitySaveFailedException;
import cn.highedu.coolshark.common.PagedVO;
import cn.highedu.coolshark.product.mapper.*;
import cn.highedu.coolshark.product.pojo.dto.SpuAddNewDTO;
import cn.highedu.coolshark.product.pojo.dto.SpuQueryParamDTO;
import cn.highedu.coolshark.product.pojo.entity.Spu;
import cn.highedu.coolshark.product.pojo.entity.SpuDetail;
import cn.highedu.coolshark.product.pojo.vo.AlbumStandardVO;
import cn.highedu.coolshark.product.pojo.vo.BrandStandardVO;
import cn.highedu.coolshark.product.pojo.vo.CategoryStandardVO;
import cn.highedu.coolshark.product.pojo.vo.SpuListItemVO;
import cn.highedu.coolshark.product.service.ISpuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;

/**
 * SPU业务实现类
 */
@Service
@Slf4j
public class SpuServiceImpl implements ISpuService {
    @Autowired
    private SpuMapper spuMapper;
    @Autowired
    private SpuDetailMapper spuDetailMapper;
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private AlbumMapper albumMapper;
    /**
     * 添加SPU
     *
     * @param spuAddNewDTO
     */
    @Override
    public void addNew(SpuAddNewDTO spuAddNewDTO) {
        log.debug("开始处理【添加SPU】的业务, 参数: {}", spuAddNewDTO);
        // 检查相册是否存在
        log.debug("检查相册是否存在");
        Long albumId = spuAddNewDTO.getAlbumId();
        AlbumStandardVO album = albumMapper.getStandardById(albumId);
        if (album == null) {
            String message = "添加SPU失败, 选择的相册不存在";
            log.warn(message);
            throw new EntityNotFoundException(message);
        }
        // 检查品牌是否存在
        log.debug("检查品牌是否存在");
        Long brandId = spuAddNewDTO.getBrandId();
        BrandStandardVO brand = brandMapper.getStandardById(brandId);
        if (brand == null) {
            String message = "添加SPU失败, 选择的品牌不存在";
            log.warn(message);
            throw new EntityNotFoundException(message);
        }
        // 检查品牌是否被禁用
        log.debug("检查品牌是否被禁用");
        if (brand.getEnable() == 0) {
            String message = "添加SPU失败, 选择的品牌已被禁用";
            log.warn(message);
            throw new EntitySaveFailedException(message);
        }
        // 检查类别是否存在
        log.debug("检查类别是否存在");
        Long categoryId = spuAddNewDTO.getCategoryId();
        CategoryStandardVO category = categoryMapper.getStandardById(categoryId);
        if (category == null) {
            String message = "添加SPU失败, 选择的类别不存在";
            log.warn(message);
            throw new EntityNotFoundException(message);
        }
        // 检查类别是否被禁用
        log.debug("检查类别是否被禁用");
        if (category.getEnable() == 0) {
            String message = "添加SPU失败, 选择的类别已被禁用";
            log.warn(message);
            throw new EntitySaveFailedException(message);
        }
        // 检查类别是否包含子类别
        log.debug("检查类别是否包含子类别");
        if (category.getIsParent() == 1) {
            String message = "添加SPU失败, 选择的类别仍包含子类别";
            log.warn(message);
            throw new EntitySaveFailedException(message);
        }
        // 创建SPU对象
        Spu spu = new Spu();
        BeanUtils.copyProperties(spuAddNewDTO, spu);
        Long spuId = Math.abs(new Random().nextLong());
        // 补全SPU对象的id属性
        spu.setId(spuId);
        // 补全SPU对象的品牌名
        spu.setBrandName(brand.getName());
        // 补全SPU对象的类别名
        spu.setCategoryName(category.getName());
        // 补全SPU对象的销量, 评论, 好评
        spu.setSales(0);
        spu.setCommentCount(0);
        spu.setPositiveCommentCount(0);
        // 补全SPU对象的是否删除, 是否发布, 是否审核
        spu.setIsDeleted(0); // 未删除
        spu.setIsPublished(0); // 未上架
        spu.setIsChecked(0); // 未审核
        // 补全SPU对象的是否是新品, 是否推荐
        spu.setIsNewArrival(1); // 是新品
        spu.setIsRecommend(1); // 推荐
        // 补全SPU对象的添加时间, 最后修改时间
        spu.setGmtCreate(LocalDateTime.now());
        spu.setGmtModified(LocalDateTime.now());
        // 插入SPU数据, 获取返回的结果并检查
        log.debug("即将插入SPU数据, 参数: {}", spu);
        int rows = spuMapper.insert(spu);
        if (rows != 1) {
            String message = "添加SPU失败, 服务器忙, 请稍后再试";
            log.warn(message);
            throw new EntitySaveFailedException(message);
        }
        // 创建商品详情(SpuDetail)对象
        SpuDetail spuDetail = new SpuDetail();
        // 补全SpuDetail对象的spuId
        spuDetail.setSpuId(spuId);
        // 补全SpuDetail对象的商品详情
        spuDetail.setDetail(spuAddNewDTO.getDetail());
        // 补全SpuDetail对象的时间
        spuDetail.setGmtCreate(LocalDateTime.now());
        spuDetail.setGmtModified(LocalDateTime.now());
        // 插入SpuDetail数据, 获取返回结果并检查
        log.debug("即将插入SPU详情数据, 参数: {}", spuDetail);
        rows = spuDetailMapper.insert(spuDetail);
        if (rows != 1) {
            String message = "添加SPU失败, 服务器忙, 请稍后再试";
            log.warn(message);
            throw new EntitySaveFailedException(message);
        }
    }

    /**
     * 分业务查询
     *
     * @param spuQueryParamDTO
     * @return
     */
    @Override
    public PagedVO<SpuListItemVO> list(SpuQueryParamDTO spuQueryParamDTO) {
        log.debug("开始查询SPU列表");
        // 查询列表详情
        List<SpuListItemVO> list = spuMapper.listByPage(spuQueryParamDTO);
        // 获取查询的条数
        int total = list.size();
        log.debug("查询到{}条SPU数据", total);
        // 封装并返回数据
        int pageNum = spuQueryParamDTO.getPageNum();
        int pageSize = spuQueryParamDTO.getPageSize();
        // 创建分页对象
        PagedVO<SpuListItemVO> pagedVO = new PagedVO<>(pageNum, pageSize, total, list);
        return pagedVO;
    }
}
